package com.yz.smartpos.yztester.util;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.yz.smartpos.yztester.ui.MainActivity;
import com.yz.smartpos.yztester.ui.ResultFragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;

public class Utils {
    public static String TAG = "utils";
    protected static MainActivity mActivity;
    private static int originStackIndex = 3;
    public static ResultFragment Logger = ResultFragment.newInstance();
    private static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle;
            switch (msg.what) {
                case 1:
                    if (mActivity != null)
                        Toast.makeText(mActivity, (String) msg.obj, msg.arg1).show();
                    break;
            }
        }
    };

    public static void chmod777Path(String path) {
        try {
            String command = "chmod -R 777 " + path;
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setActivity(Activity activity){
        mActivity = (MainActivity) activity;
    }

    public static void showToast(String msg, int type) {
        showToast(mActivity, msg, type);
    }
    public static void showToast(String msg) {
        showToast(mActivity, msg, Toast.LENGTH_LONG);
    }
    public static void showToast(Context context, String msg, int type) {
        mActivity = (MainActivity) context;
        Message message = mHandler.obtainMessage(1);
        message.obj = msg;
        message.arg1 = type;
        mHandler.sendMessage(message);
    }

    public static void sleep(int s){
        try {
            Thread.sleep(s*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void msleep(int ms){
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void saveTestResult() throws IOException {
        TesterConfig.result_log_out.flush();
        TesterConfig.result_pm_out.flush();
    }

    /**
     * 将asset的文件写入到sd卡目录
     * @param file_name
     */
    public static void asset2sd(String path, String file_name, String file_name2) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), file_name2);
        Log.i(TAG, "asset2sd:" + Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        try {
            if (!file.exists()) {
                InputStream stream = mActivity.getAssets()
                        .open(path + "/" + file_name);
                OutputStream os = new FileOutputStream(file);
                byte[] buffer = new byte[1024 * 8];
                int read;
                while ((read = stream.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                stream.close();
                os.close();
            } else {
                Log.e(TAG, "file has exists:" + file_name2);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 将asset的文件写入到sd卡目录
     * @param file_name
     */
    public static void asset2sd(String path, String file_name) {
        asset2sd(path, file_name, file_name);
    }

    public static long[] TimeCount(ArrayList<Long> puiTime) {
        long temp, puiMax, puiMin, puiAvg;

        puiMax = puiTime.get(0);
        puiMin = puiTime.get(0);
        temp = 0;
        for (long i : puiTime) {
            temp += i;
            if (puiMax < i) {
                puiMax = i;
            }
            if (puiMin > i) {
                puiMin = i;
            }
        }
        puiAvg = (temp - puiMax - puiMin) / (puiTime.size() - 2);
        return new long[]{puiAvg, puiMax, puiMin };
    }

    public static void printException(Exception e){
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        Log.e(TAG, stringWriter.toString());
        Logger.appendLog(stringWriter.toString());
    }

}
